package com.itboye.hutouben.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.DividerDecoration;
import com.itboye.hutouben.adapter.SingleAdapter;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.bean.PersonDataBean;
import com.itboye.hutouben.support.myrefreshrecyclerview.ProgressStyle;
import com.itboye.hutouben.support.myrefreshrecyclerview.XRecyclerView;
import com.itboye.hutouben.testitemclick.SingleItemClickListener;
import com.itboye.hutouben.util.ByAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewTest extends BaseActivity {

    XRecyclerView chat_rv;
    private SingleAdapter singleAdapter;
    private LinearLayoutManager layoutManager;
    private int refreshTime;
    private int times;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_test);
        singleAdapter = new SingleAdapter(this);
        singleAdapter.fillList(getPersonList());
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        chat_rv.setLayoutManager(layoutManager);
        DividerDecoration decoration = new DividerDecoration(this, DividerDecoration.VERTICAL_LIST);
        Drawable drawable = getResources().getDrawable(R.drawable.divider_single);
        decoration.setDivider(drawable);
        chat_rv.addItemDecoration(decoration);
        chat_rv.setAdapter(singleAdapter);

        chat_rv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        chat_rv.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        chat_rv.setArrowImageView(R.drawable.iconfont_downgrey);
        View view = LayoutInflater.from(this).inflate(R.layout.item_single_header, null, false);
        singleAdapter.addHeaderView(view);
        chat_rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                refreshTime ++;
                times = 0;
                new Handler().postDelayed(new Runnable(){
                    public void run() {

                        chat_rv.refreshComplete();
                    }

                }, 1000);            //refresh data here
            }

            @Override
            public void onLoadMore() {
                if(times < 2){
                    new Handler().postDelayed(new Runnable(){
                        public void run() {

                            chat_rv.loadMoreComplete();

                        }
                    }, 1000);
                } else {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

                            chat_rv.setNoMore(true);

                        }
                    }, 1000);
                }
                times ++;
            }
        });
        chat_rv.addOnItemTouchListener(new SingleItemClickListener(chat_rv,
                new SingleItemClickListener.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, int position) {

                        ByAlert.alert("touch click:" + position);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        ByAlert.alert("touch long click:" + position);
                    }
                }));
    }

    public List<PersonDataBean> getPersonList() {
        Random random = new Random();
        List<PersonDataBean> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            PersonDataBean person = new PersonDataBean();
            person.setUsername("name-" + i);
            person.setUid(""+random.nextInt(30));
            person.setSex(i%2+"");
            list.add(person);
        }
        return list;
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {

    }
}
